package com.shxy.rlzy.controller;


import cn.hutool.json.JSONObject;
import com.shxy.rlzy.common.lang.Result;
import com.shxy.rlzy.controller.salary.BonusItem;
import com.shxy.rlzy.entity.EmployeeInfo;
import com.shxy.rlzy.entity.SalaryItem;
import com.shxy.rlzy.entity.User;
import com.shxy.rlzy.service.IDepartService;
import com.shxy.rlzy.service.IEmployeeInfoService;
import com.shxy.rlzy.service.ISalaryItemService;
import com.shxy.rlzy.service.ISalaryTaskDepartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tanhao
 * @since 2022-03-19
 */
@RestController
@RequestMapping("/salary_item")
public class SalaryItemController extends BaseController {
    @Autowired
    private ISalaryItemService salaryItemService;
    @Autowired
    private ISalaryTaskDepartService salaryTaskDepartService;
    @Autowired
    private IEmployeeInfoService employeeInfoService;
    @Autowired
    private IDepartService departService;

    /**
     * 获取部门人员绩效列表
     * @param taskDepartId
     * @return
     */
    @GetMapping("/list/depart/{taskDepartId}")
    public Result getDepartSalaryItems(@PathVariable long taskDepartId) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = (String) authentication.getPrincipal();
        // 1、获取部门工资员对应的用户名
        Long departId = salaryTaskDepartService.getDepartIdByTaskId(taskDepartId);
        User clerk = departService.getDepartClerk(departId);

        if(clerk == null || !clerk.getUsername().equals(username)) {
            return Result.fail("对不起，你无权获取该部门信息");
        }

        // 2、查询部门绩效人员列表
        List<SalaryItem> items = salaryItemService.getTaskDepartItems(taskDepartId);
        List<JSONObject> list = items.stream().map(this::convertItems).collect(Collectors.toList());
        return Result.succ(list);
    }

    /**
     * 保存绩效
     * @param id
     * @param bonus
     * @return
     */
    @PutMapping("/set_bonus/{id}/{bonus}")
    @PreAuthorize("hasAnyAuthority('wage:bonus:save')")
    public Result setBonus(@PathVariable Long id, @PathVariable double bonus) {
        boolean res = salaryItemService.saveBonus(id, bonus);
        return Result.succ(res);
    }

    /**
     * 保存绩效数据
     * @param items
     * @return
     */
    @PutMapping("/saves")
    @PreAuthorize("hasAnyAuthority('wage:bonus:save')")
    public Result saveBonus(@RequestBody List<BonusItem> items) {
        List<SalaryItem> salaryItems = items.stream().map(item -> {
            SalaryItem si = new SalaryItem();
            si.setId(item.getId());
            si.setMonthMerit(item.getBonus());
            return si;
        }).collect(Collectors.toList());
        int n = salaryItemService.setBonusList(salaryItems);
        return Result.succ("上传成功");
    }

    /**
     * 格式转换
     * @param salaryItem
     * @return
     */
    private JSONObject convertItems(SalaryItem salaryItem) {
        JSONObject obj = new JSONObject();
        obj.set("id", salaryItem.getId());
        obj.set("name", salaryItem.getEmployeeName());
        obj.set("employeeId", salaryItem.getEmployeeId());
        obj.set("bonus", salaryItem.getMonthMerit());
        obj.set("taskDepartId", salaryItem.getTaskDepartId());
        obj.set("statu", salaryItem.getStatu());
        return obj;
    }
}
